Imports System.Drawing
Imports System.Windows.Forms
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Codecs

Namespace Imaging
	Public Partial Class ThumbnailViewerSettingsForm
		Inherits Form

		#Region "Fields"

		Private _viewer As ThumbnailViewer

		Private _thumbnailAppearance As ThumbnailAppearance

		Private _focusedThumbnailAppearance As ThumbnailAppearance

		Private _hoveredThumbnailAppearance As ThumbnailAppearance

		Private _selectedThumbnailAppearance As ThumbnailAppearance

		Private _notReadyThumbnailAppearance As ThumbnailAppearance

		#End Region



		#Region "Constructors"

		Public Sub New()
			InitializeComponent()

			thumbnailFlowStyleComboBox.Items.Add(ThumbnailFlowStyle.WrappedRows)
			thumbnailFlowStyleComboBox.Items.Add(ThumbnailFlowStyle.SingleRow)
			thumbnailFlowStyleComboBox.Items.Add(ThumbnailFlowStyle.SingleColumn)
			thumbnailFlowStyleComboBox.Items.Add(ThumbnailFlowStyle.FixedColumns)

			thumbnailScaleComboBox.Items.Add(ThumbnailScale.Smallest)
			thumbnailScaleComboBox.Items.Add(ThumbnailScale.Small)
			thumbnailScaleComboBox.Items.Add(ThumbnailScale.Normal)
			thumbnailScaleComboBox.Items.Add(ThumbnailScale.Large)

			thumbnailAppearanceComboBox.Items.Add("Normal")
			thumbnailAppearanceComboBox.Items.Add("Focused")
			thumbnailAppearanceComboBox.Items.Add("Hovered")
			thumbnailAppearanceComboBox.Items.Add("Selected")
			thumbnailAppearanceComboBox.Items.Add("Not ready")
		End Sub

		Public Sub New(viewer As ThumbnailViewer)
			Me.New()
			_viewer = viewer
			_thumbnailAppearance = New ThumbnailAppearance(viewer.ThumbnailAppearance)
			_focusedThumbnailAppearance = New ThumbnailAppearance(viewer.FocusedThumbnailAppearance)
			_hoveredThumbnailAppearance = New ThumbnailAppearance(viewer.HoveredThumbnailAppearance)
			_selectedThumbnailAppearance = New ThumbnailAppearance(viewer.SelectedThumbnailAppearance)
			_notReadyThumbnailAppearance = New ThumbnailAppearance(viewer.NotReadyThumbnailAppearance)
			thumbnailAppearanceComboBox.SelectedIndex = 0
			ShowSettings()
		End Sub

		#End Region



		#Region "Methods"

		Private Sub ShowSettings()
			generateOnlyVisibleThumbnailsCheckBox.Checked = _viewer.GenerateOnlyVisibleThumbnails
			thumbnailSizeComboBox.Text = [String].Format("{0} x {1}", _viewer.ThumbnailSize.Width, _viewer.ThumbnailSize.Height)
			thumbnailFlowStyleComboBox.SelectedItem = _viewer.ThumbnailFlowStyle
			thumbnailColumnsCountComboBox.SelectedIndex = _viewer.ThumbnailFixedColumnCount - 1
			thumbnailScaleComboBox.SelectedItem = _viewer.ThumbnailScale
			InitThumbnailViewerBackColor(_viewer.BackColor)
		End Sub

		Private Function SetSettings() As Boolean
			_viewer.GenerateOnlyVisibleThumbnails = generateOnlyVisibleThumbnailsCheckBox.Checked

			' Thumbnail Size
			Try
				Dim sizeStrings As String() = thumbnailSizeComboBox.Text.Split("x"C)
				Dim width As Integer
				Dim height As Integer
				If sizeStrings.Length = 1 Then
					width = Convert.ToInt32(sizeStrings(0))
					height = width
				Else
					width = Convert.ToInt32(sizeStrings(0))
					height = Convert.ToInt32(sizeStrings(1))
				End If
				_viewer.ThumbnailSize = New Size(width, height)
			Catch e As Exception
				DemosTools.ShowErrorMessage(e)
				Return False
			End Try

			If DirectCast(thumbnailFlowStyleComboBox.SelectedItem, ThumbnailFlowStyle) = ThumbnailFlowStyle.FixedColumns Then
				_viewer.ThumbnailFixedColumnCount = CInt(thumbnailColumnsCountComboBox.SelectedIndex) + 1
			End If
			_viewer.ThumbnailFlowStyle = DirectCast(thumbnailFlowStyleComboBox.SelectedItem, ThumbnailFlowStyle)
			_viewer.ThumbnailScale = DirectCast(thumbnailScaleComboBox.SelectedItem, ThumbnailScale)
			_viewer.BackColor = thumbnailViewerBackColorPanel.BackColor

			_viewer.ThumbnailAppearance = _thumbnailAppearance
			_viewer.FocusedThumbnailAppearance = _focusedThumbnailAppearance
			_viewer.HoveredThumbnailAppearance = _hoveredThumbnailAppearance
			_viewer.SelectedThumbnailAppearance = _selectedThumbnailAppearance
			_viewer.NotReadyThumbnailAppearance = _notReadyThumbnailAppearance

			Return True
		End Function

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			If SetSettings() Then
				DialogResult = DialogResult.OK
			End If
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub thumbnailAppearanceComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			Select Case thumbnailAppearanceComboBox.SelectedIndex
				Case 0
					InitAppearance(_thumbnailAppearance)
					Exit Select
				Case 1
					InitAppearance(_focusedThumbnailAppearance)
					Exit Select
				Case 2
					InitAppearance(_hoveredThumbnailAppearance)
					Exit Select
				Case 3
					InitAppearance(_selectedThumbnailAppearance)
					Exit Select
				Case 4
					InitAppearance(_notReadyThumbnailAppearance)
					Exit Select
			End Select
		End Sub

		Private Sub InitAppearance(thumbnailAppearance As ThumbnailAppearance)
			Dim backColor As Color = thumbnailAppearance.BackColor
			backColorPanel.BackColor = backColor
			If Math.Abs(backColor.R - 128) < 30 AndAlso Math.Abs(backColor.G - 128) < 30 AndAlso Math.Abs(backColor.B - 128) < 30 Then
				backColorValueLabel.ForeColor = Color.White
			Else
				backColorValueLabel.ForeColor = Color.FromArgb(&Hffffff Xor backColor.ToArgb())
			End If
			If backColor.IsNamedColor Then
				backColorValueLabel.Text = backColor.Name
			Else
				backColorValueLabel.Text = ToHtmlColor(backColor)
			End If

			Dim borderColor As Color = thumbnailAppearance.BorderColor
			borderColorPanel.BackColor = borderColor
			If Math.Abs(borderColor.R - 128) < 30 AndAlso Math.Abs(borderColor.G - 128) < 30 AndAlso Math.Abs(borderColor.B - 128) < 30 Then
				borderColorValueLabel.ForeColor = Color.White
			Else
				borderColorValueLabel.ForeColor = Color.FromArgb(&Hffffff Xor borderColor.ToArgb())
			End If
			If borderColor.IsNamedColor Then
				borderColorValueLabel.Text = borderColor.Name
			Else
				borderColorValueLabel.Text = ToHtmlColor(borderColor)
			End If

			borderWidthValueLabel.Text = thumbnailAppearance.BorderWidth.ToString()
			borderStyleValueLabel.Text = thumbnailAppearance.BorderStyle.ToString()
		End Sub

		Private Sub editThumbnailAppearanceButton_Click(sender As Object, e As EventArgs)
			Dim appearance As ThumbnailAppearance = Nothing
			Select Case thumbnailAppearanceComboBox.SelectedIndex
				Case 0
					appearance = _thumbnailAppearance
					Exit Select
				Case 1
					appearance = _focusedThumbnailAppearance
					Exit Select
				Case 2
					appearance = _hoveredThumbnailAppearance
					Exit Select
				Case 3
					appearance = _selectedThumbnailAppearance
					Exit Select
				Case 4
					appearance = _notReadyThumbnailAppearance
					Exit Select
			End Select

			Dim thumbnailAppearanceSettingsDialog As New ThumbnailAppearanceSettingsForm(appearance)

			If thumbnailAppearanceSettingsDialog.ShowDialog() = DialogResult.OK Then
				InitAppearance(appearance)
			End If
		End Sub

		Private Sub thumbnailFlowStyleComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			If DirectCast(thumbnailFlowStyleComboBox.SelectedItem, ThumbnailFlowStyle) = ThumbnailFlowStyle.FixedColumns Then
				thumbnailColumnsCountComboBox.Enabled = True
			Else
				thumbnailColumnsCountComboBox.Enabled = False
			End If
		End Sub

		Private Sub thumbnailViewerBackColorButton_Click(sender As Object, e As EventArgs)
			thumbnailViewerBackColorDialog.Color = thumbnailViewerBackColorPanel.BackColor
			If thumbnailViewerBackColorDialog.ShowDialog() = DialogResult.OK Then
				InitThumbnailViewerBackColor(thumbnailViewerBackColorDialog.Color)
			End If
		End Sub

		Private Sub InitThumbnailViewerBackColor(color__1 As Color)
			thumbnailViewerBackColorPanel.BackColor = color__1
			If Math.Abs(color__1.R - 128) < 30 AndAlso Math.Abs(color__1.G - 128) < 30 AndAlso Math.Abs(color__1.B - 128) < 30 Then
				thumbnailViewerBackColorValueLabel.ForeColor = Color.White
			Else
				thumbnailViewerBackColorValueLabel.ForeColor = Color.FromArgb(&Hffffff Xor color__1.ToArgb())
			End If
			If color__1.IsNamedColor Then
				thumbnailViewerBackColorValueLabel.Text = color__1.Name
			Else
				thumbnailViewerBackColorValueLabel.Text = ToHtmlColor(color__1)
			End If
		End Sub

		Private Function ToHtmlColor(color As Color) As String
			Return [String].Format("#{0}{1}{2}", color.R.ToString("X2"), color.G.ToString("X2"), color.B.ToString("X2"))
		End Function

		#End Region

	End Class
End Namespace
